# -*- coding: utf-8 -*-
import scrapy
import logging
from scrapy import Spider, Request,Selector
from ..items import ZufangItem
import json
import re


class ZufangmsgSpider(scrapy.Spider):

    name = 'zufangmsg'
    allowed_domains = ['zu.fang.com']
    start_urls = ['https://xian.zu.fang.com/']

    def parse(self, response):
        # selector = Selector(response)
        fanye_sum = response.xpath(".//*[@class='fanye']/span/text()").extract()[0]
        fanye_sum = re.findall(r"\d+", fanye_sum)[0]
        # pageNow = response.xpath(".//*[@class='fanye']/@class='pageNow'").extract()[0]
        for page in range(1, int(fanye_sum)):
            if page == 1:
                url = self.start_urls[0]
            else:
                url = response.url + 'house/i3' + str(page)
            print(url)
            # yield Request(url=url, callback=self.parse_item, dont_filter=True)
            yield Request(url=url, callback=self.parse_item)

        # logging.info(fanye_sum)

    def parse_item(self,response):
        # pass
        item = ZufangItem()
        houses = response.xpath(".//*[@class='list hiddenMap rel']")
        for house in houses:
            name = house.xpath(".//*[@class='title']/a/@title").extract_first()
            # print(name)
            if name is None:    # 跳过第十条信息--二维码部分
                continue
            area = house.xpath(".//*[@class='gray6 mt12']/a/span/text()").extract()

            house_msg = house.xpath(".//*[@class='font15 mt12 bold']/text()").extract()
            house_msg = [x.strip() for x in house_msg]

            item["name"] = name
            item["mode"] = house_msg[0]             # 方式
            item["house_type"] = house_msg[1]      # 户型
            item["size"] = house_msg[2]             # 大小
            item["area"] = '-'.join(area)    #位置
            item["rental"] = house.xpath(".//*[@class='moreInfo']/p/span/text()").extract_first()  # 租金
            yield item





